﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Connection
{
    class ConnectDB : MarshalByRefObject
    {
        private string connectionString;
        private MySqlConnection connection = null;

        public string getConnectionString()
        {
            connectionString = "server=localhost;" + "database=dbchat;" + "userid=root;" + "password=123456";
            return connectionString;
        }

        public int QueryData(string cmdtext)
        {
            //setConnectionString();
            connection = new MySqlConnection(getConnectionString());
            MySqlCommand cmd = new MySqlCommand(cmdtext, connection);
            int kt = -1;

            try
            {
                //mo ket noi
                connection.Open();

                //thuc hien cau truy van thong qua MySqlCommand
                kt = cmd.ExecuteNonQuery();
                return kt;
            }
            catch (MySqlException err)
            {
                MessageBox.Show("Error:" + err.ToString());
                return kt;
            }
            finally
            {
                //dong ket noi 
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }
    }
}
